import { OPENENUM, SENSITIVEENUM } from '@/enum';
import { Form, Input, Modal, Select } from 'antd';
import { useEffect } from 'react';
import hooks from '../../hooks';
const normFile = (e: any) => {
  if (Array.isArray(e)) {
    return e;
  }
  return e?.fileList;
};
export default () => {
  const {
    modalVisible,
    setModalVisible,
    tabsType,
    addResourceGroupData,
    resourceData,
    formResource,
    setResourceData,
    tabKey,
    editResourceGroupData,
  } = hooks.useContainer();
  const tabName = tabKey === 'file' ? '资源' : '知识库';

  const handleSubmit = () => {
    formResource.validateFields().then((vals) => {
      const method = resourceData ? editResourceGroupData : addResourceGroupData;

      method.run({
        ...vals,
      });
    });
  };

  useEffect(() => {
    if (modalVisible && resourceData) {
      formResource.setFieldsValue({
        ...(resourceData as Record<string, any>),
      });
    } else {
      formResource.resetFields();
      setResourceData(null);
    }
  }, [resourceData, modalVisible]);
  return (
    <Modal
      width={600}
      title={`${resourceData ? '修改' : '创建'}${tabsType[tabKey].name}`}
      open={modalVisible}
      onCancel={() => {
        setModalVisible(false);
      }}
      okText="确认"
      onOk={handleSubmit}
    >
      <Form form={formResource} labelCol={{ span: 6 }}>
        <Form.Item name="id" hidden />

        <Form.Item name="resourceName" label={`${tabName}名称`} rules={[{ required: true }]}>
          <Input placeholder="请输入"></Input>
        </Form.Item>

        <Form.Item
          label={`${tabName}公开属性`}
          name="openProperty"
          rules={[{ required: true, message: '请选择' }]}
        >
          <Select disabled={resourceData ? true : false} placeholder="请选择">
            <Select.Option value={OPENENUM.PUBLICP}>无条件公开</Select.Option>
            <Select.Option value={OPENENUM.DEPT}>部门公开</Select.Option>
            <Select.Option value={OPENENUM.PROTECTED}>有条件公开</Select.Option>
            <Select.Option value={OPENENUM.PRIVATE}>不予公开</Select.Option>
          </Select>
        </Form.Item>
        <Form.Item
          label={`${tabName}敏感等级`}
          name="sensitiveLevel"
          rules={[{ required: true, message: '请选择敏感等级' }]}
        >
          <Select disabled={resourceData ? true : false} placeholder="请选择">
            <Select.Option value={SENSITIVEENUM.NO_SENSITIVE}>L1不敏感</Select.Option>
            <Select.Option value={SENSITIVEENUM.LOW_SENSITIVE}>L2低敏感</Select.Option>
            <Select.Option value={SENSITIVEENUM.LITTLE_SENSITIVE}>L3较敏感</Select.Option>
            <Select.Option value={SENSITIVEENUM.SENSITIVE}>L4敏感</Select.Option>
          </Select>
        </Form.Item>
        <Form.Item name="description" label="描述" rules={[{ required: true }]}>
          <Input.TextArea placeholder="请输入内容"></Input.TextArea>
        </Form.Item>
      </Form>
    </Modal>
  );
};
